package com.whut.welcomepage.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.whut.welcomepage.MovieBean;
import com.whut.welcomepage.R;
import com.whut.welcomepage.fragment.HttpURLConnHelper;

import java.util.List;

/**
 * Created by Noone on 2016/8/27.
 * Today is today!
 */
public class ListViewAdapter_Main extends BaseAdapter {
    private Handler handler = new Handler();
    private Context mContext;
    private List<MovieBean.ListBean> totalList;
    private LayoutInflater inflater;

    public ListViewAdapter_Main(Context context, List<MovieBean.ListBean> list) {
        mContext = context;
        totalList = list;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        Log.i("ListViewAdapter_Main", "------>getCount:"+totalList.size());
        return totalList.size();

    }

    @Override
    public Object getItem(int position) {
        return totalList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.i("ListViewAdapter_Main", "------>getView:");
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView_item_name.setText(totalList.get(position).getName());
        viewHolder.textView_item_actor.setText(totalList.get(position).getActors());
        viewHolder.textView_item_highlight.setText(totalList.get(position).getHighlight());
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] bytes = HttpURLConnHelper.getByteArrayFromNet(totalList.get(position).getLogo());
                final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        viewHolder.imageView_item.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();
        return convertView;
    }


    class ViewHolder {
        private ImageView imageView_item;
        private TextView textView_item_name;
        private TextView textView_item_highlight;
        private TextView textView_item_actor;

        public ViewHolder(View view) {
            imageView_item = (ImageView) view.findViewById(R.id.imageView_item);
            textView_item_name = (TextView) view.findViewById(R.id.textView_item_name);
            textView_item_actor = (TextView) view.findViewById(R.id.textView_item_actor);
            textView_item_highlight = (TextView) view.findViewById(R.id.textView_item_highlight);
        }
    }

}
